<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends CommenController {

function lists(){


    $list =$this->Model->relation(true)->select();
    // print_r($list);die;

    $this->assign('list',$list);
    $this->display();

}


  function add(){
// s('spec_item',null);
 if(IS_POST){
        //D ==NEW 实例化
 // print_r($_FILES) ;die;

    $_POST['img']=$this->upload();
    // print_r($_POST);die;
      unset($_POST['price']);
      unset($_POST['spec_num']);

    $_POST['goods_no']=date('Ymd',time()).mt_rand(1000,9999);
    // print_r($_POST);die;
       // unset($_POST['group_id']);
     $id=$this->Model->add(I('post.'));


        if ($id) {
//添加商品规格价格表

            foreach (s('spec_item') as $key => $v) {
                $v['spec_rule']=$id.$v['spec_rule'];

                $v['goods_id']=$id;
                // print_r($v);
                D('spec_price')->add($v);
                // echo  D('spec_price')->getlastsql();die;
                # code...
            }
         $this->success('操作完成',U('Admin/goods/lists'),1);
        }else{
        $this->error('操作失败','',2);
             }


      }
      s('spec_item',null);
    $goods_type =D('brand')->select();
    $this->assign('brand',$goods_type);// 赋值分页输出

    $goods_type =D('cate')->select();
    $this->assign('cate',$goods_type);// 赋值分页输出


    $this->display();
}



Public function upload(){
    // print_r($_FILES['img']) ;die;
// import('ORG.Net.UploadFile');
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize  = 3145728 ;// 设置附件d上传大小
$upload->exts   = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->savePath =  '';// 设置附件上传目录
$upload->autoSub = true;

$upload->subName = array('date','Ymd');
 $info=$upload->uploadOne($_FILES['img']);
// print_r($_FILES['img']) ;die;
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功 获取上传文件信息


return 'Uploads/'.$info['savepath'].$info['savename'];
 // echo 'Uploads/'.$info['savepath'].$info['savename'];die;
}

}


  function goods_img(){

        // print_r($_FILES['img']) ;die;
// import('ORG.Net.UploadFile');
if (!empty($_FILES['files'])) {
    # code...

$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize  = 3145728 ;// 设置附件d上传大小
$upload->exts   = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->savePath =  '';// 设置附件上传目录
$upload->autoSub = true;

$upload->subName = array('date','Ymd');
 $info=$upload->uploadOne($_FILES['files']);
// print_r($_FILES['img']) ;die;
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功 获取上传文件信息


$data['url'] ='Uploads/'.$info['savepath'].$info['savename'];
$data['goods_id']=I('post.g_id');

 /* print_r($data);die;*/
D('goods_img')->add($data);
// print_r(D('goods_img')->getlastsql());die;
 // echo 'Uploads/'.$info['savepath'].$info['savename'] ;die;
}
}
      $this->display();

}

        }